import '../css/common.css'
import '../css/queue.less'
import Hls from 'hls'
import sendMessage from '../common/sendMessage/message'
import qs from './common/qs'
import {getSiteInfo} from './common/api'
let navigationDom = document.getElementById('navigation')
qs.urlParse() && (navigationDom.href = 'navigation.html?id=' + qs.urlParse().id)
getSiteInfo(qs.urlParse())
  .then(data=>{
    // 视频播放按钮DOM
    let videoPalyBtn = document.getElementById('videoPalyBtn')
    // 视频标签DOM
    let queueVideo = document.getElementById('queueVideo')
    // 点击视频播放按钮，开始播放
    videoPalyBtn.addEventListener('click',function(){
      videoPalyBtn.setAttribute('playing',true)
      queueVideo.play()
    })
    // 监听播放结束
    queueVideo.addEventListener('ended',()=>{
      videoPalyBtn.setAttribute('playing',false)
    })
    // 监听暂停播放
    queueVideo.addEventListener('pause',()=>{
      videoPalyBtn.setAttribute('playing',false)
    })
    // 监听播放出错
    queueVideo.addEventListener('error',()=>{
      videoPalyBtn.setAttribute('video-error',true)
    })
    // 监听开始播放
    queueVideo.addEventListener('timeupdate',()=>{
      videoPalyBtn.setAttribute('playing',true)
    })
    
    // 点击视频标签，暂停播放
    queueVideo.addEventListener('click',()=>{
      videoPalyBtn.setAttribute('playing',false)
      queueVideo.pause()
    })
    // 'http://hls.open.ys7.com/openlive/084625d832414eae8605e0e33e9bb11c.hd.m3u8'
    // data.video = 'http://hls.open.ys7.com/openlive/084625d832414eae8605e0e33e9bb11c.hd.m3u8'
    if(data.video) { // 如果当前站点有配置视频地址，开始使用Hls插件播放
      if(Hls.isSupported()) {
        var hls = new Hls()
        hls.loadSource(data.video )
        hls.attachMedia(queueVideo)
        // hls.on(Hls.Events.MANIFEST_PARSED,function() {
        //   queueVideo.play()
        // })
      }
      else if (queueVideo.canPlayType('application/vnd.apple.mpegurl')) {
        queueVideo.src = data.video
        // queueVideo.addEventListener('canplay',function() {
        //   queueVideo.play()
        // })
      }
    } else { // 否则提示没有配置视频地址
      document.body.setAttribute('non-video','true')
    }

  })
  .catch(err=>{
    sendMessage({msg:`服务器出错，请稍后重试！${err}`})
  })